package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author 刘付磊
 * @date 2021/11/13 0013
 */
@Service
public class SettingsService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        //1.获取用户id
        Long userId = UserHolder.getUserId();
        vo.setId(userId);
        //2.获取用户手机号
        String mobile = UserHolder.getMobile();
        vo.setPhone(mobile);
        //3.获取用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "你喜欢java吗?" : question.getTxt();
        //4.获取用户的APP通知数据
        Settings settings = settingsApi.findByUserId(userId);
        vo.setStrangerQuestion(txt);
        if (settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }
        return vo;
    }

    //设置陌生人问题
    public void saveQuestions(String content) {
        //1.获取当前用户id
        Long userId = UserHolder.getUserId();
        //2.调用api查询当前用户的陌生人问题
        Question byUserId = questionApi.findByUserId(userId);
        //3.判断问题是否存在
        if (byUserId == null) {
            //3.1如果不存在，保存
            byUserId = new Question();
            byUserId.setUserId(userId);
            byUserId.setTxt(content);
            questionApi.save(byUserId);
        } else {
            //3.2如果存在 修改更新
            byUserId.setTxt(content);
            questionApi.update(byUserId);
        }
    }

    public void saveSettings(Map map) {
        boolean gonggaoNotification = (boolean) map.get("gonggaoNotification");
        boolean LikeNotification = (boolean) map.get("likeNotification");
        boolean PinglunNotification = (boolean) map.get("pinglunNotification");
        //1.获取当前用户id
        Long userId = UserHolder.getUserId();
        //2.根据用户id，查询用户通知设置
        Settings settings = settingsApi.findByUserId(userId);
        //3.判断
        if(settings==null){
            settings = new Settings();
            settings.setUserId(userId);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(LikeNotification);
            settings.setPinglunNotification(PinglunNotification);
            settingsApi.save(settings);
        }else {
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(LikeNotification);
            settings.setPinglunNotification(PinglunNotification);
            settingsApi.update(settings);
        }
    }

    public PageResult blacklist(int page, int size) {
        //1.获取当前用户id
        Long userId = UserHolder.getUserId();
        //2.调用aip查询用户的黑名单分页列表,Ipage对象
        IPage<UserInfo> pg= blackListApi.findByUser(userId,page,size);

        //3.转化对象，将查询的Ipage对象的内容封装到pageresult中
        PageResult pr= new PageResult(page,size, (int) pg.getTotal(),pg.getRecords());

        //4.返回
        return pr;
    }

    public void deleteblacklist(Long blackUserId) {
        //1.获取当前用户id
        Long userId = UserHolder.getUserId();
        //2.调用api删除
        blackListApi.delete(userId,blackUserId);
    }
}
